#include "text_edit.h"
#include "ui_text_edit.h"
#include "www_data.h"

text_edit::text_edit(QWidget *parent)
    : QDialog(parent), ui(new Ui::text_edit)
{
    ui->setupUi(this);
    ui->contain_image->setIconSize(QSize::QSize(30,30));
    image_signal = 0;
}

text_edit::~text_edit()
{
    delete ui;
}

//create text editor view containing contents.
void text_edit::text_view(QList<QTableWidgetItem*> item)//parameter come from mainwindow
{
    ui->text_edit_title->insert(item.at(0)->text());
    ui->text_edit_date->setText(item.at(1)->text());
    ui->text_edit_content->setText(item.at(2)->text());

    connect(ui->text_edit_title, SIGNAL(textChanged(QString)), this, SLOT(text_edit_available()));
    connect(ui->text_edit_content, SIGNAL(textChanged()), this, SLOT(text_edit_available()));
    connect(ui->contain_image, SIGNAL(currentIndexChanged(QString)), this, SLOT(text_edit_available()));
    check = 0;
}
//add items to image list (Qcombobox)
void text_edit::containing_image_view(QList<QTableWidgetItem*> image_list)
{
    ui->contain_image->addItem("---select image---", Qt::UserRole);
    if(image_list.count()!=0){
        for(int i = 0; i < image_list.count(); i++){
            QVariant item = qVariantFromValue(*image_list.at(i));
            ui->contain_image->addItem(image_list.at(i)->icon(), image_list.at(i)->text(), item);
        }
    }
}
void text_edit::containing_image_set(QString choosen_image)
{
    int index = ui->contain_image->findText(choosen_image, Qt::MatchExactly);
    ui->contain_image->setCurrentIndex(index);
}
void text_edit::text_close()
{
    check = 0;
    this->close();
}

//when save button is clicked
void text_edit::text_save()
{    
    //if title is not exist
    if(ui->text_edit_title->text().compare("") == 0)
    {
        check = 0;
        QMessageBox::about(this,tr("Text View Error"),tr("Input at least title."));
    }
    //if title exists
    else
    {
        check = 1;
        this->close();
    }
}

//when edit button is clicked
void text_edit::text_edit_button()
{
    check = 2;
    this->close();
}



void text_edit::text_image_view()
{
    image_signal = 1;
}


//return contents as QStirngList
QStringList text_edit::text_return()
{
    QStringList contents;
    contents.append(ui->text_edit_title->text());
    contents.append(ui->text_edit_content->toPlainText());
    return contents;
}

//return choosed image
QTableWidgetItem text_edit::image_return()
{
    int index = ui->contain_image->currentIndex();

    QTableWidgetItem item;
    item.setData(0, ui->contain_image->itemText(index));
    item.setIcon(ui->contain_image->itemIcon(index));    
    return item;
}
//SAVE button can't be used
void text_edit::text_save_enable()
{
    ui->text_edit_save->setEnabled(false);
}
//EDIT button can't be used
void text_edit::text_edit_enable()
{
    ui->text_edit_edit->setEnabled(false);
}
//if title exist, then EDIT button can be used
void text_edit::text_edit_available()
{
    if(ui->text_edit_title->text().size()!=0)
        ui->text_edit_edit->setEnabled(true);
    else{
        QMessageBox::about(this,tr("Text Editor"),tr("Must insert a title."));
        text_edit_enable();
    }
}
